package ytdp.careagle.com.ytdp.myview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import java.util.Calendar;
import java.util.Date;

import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.utils.DateUtils;
import ytdp.careagle.com.ytdp.utils.Utils;

/**
 * Created by admin on 2017/9/22.
 */

public class MyChartLine extends View {

    private DashPathEffect dashPathEffect;
    private Path linePath;
    private Paint paint;
    private TextPaint textPaint;
    private int textWidthMin;
    private int textHeightMin;
    private int textColor;
    private float sp10;
    private int textWidthBig;
    private int textHeightBig;
    private int paddingTop = 0;
    private int paddingLeft = 0;
    private int paddingBottom = 0;
    private int paddingRight = 0;
    private int year;
    private String timePoint1;
    private String timePoint4;
    private String timePoint2;
    private String timePoint3;
    private String price1;
    private String price2;
    private String price3;
    private String price4;
    private int top;
    private int left;
    private int right;
    private int bottom;
    private int with;
    private int height;
    private int with0;
    private int with1;
    private int with2;
    private int with3;
    private int height0;
    private int height1;
    private int height2;
    private int height3;
    private float currPriceHeight;
    private float nextYearPriceHeight;
    private float currPrice;
    private float nextYearPrice;
    private float newCarGuidePrice;
    private Paint priceLinePaint;
    private float radius;
    private int lineColor;
    private float radiusInner;
    private Path pathRes;
    private Path pathDst;
    private float lineLength;
    private PathMeasure measure;
    private Paint linePaint;
    private boolean isDrawed = false;//是否已经绘制了
    private ValueAnimator valueAnimator;
    private float textTran;

    public MyChartLine(Context context) {
        this(context, null);
    }

    public MyChartLine(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        pathRes = new Path();
        pathDst = new Path();

    }

    private void init() {
        linePath = new Path();
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(1);
        paint.setColor(getResources().getColor(R.color.line_color));
        paint.setAntiAlias(true);
        float dashPathEffectWith = getResources().getDimension(R.dimen.dp2);
        dashPathEffect = new DashPathEffect(new float[]{dashPathEffectWith, dashPathEffectWith / 2}, 1);
        paint.setPathEffect(dashPathEffect);
        measureText("2015/12");
        priceLinePaint = new Paint();
        priceLinePaint.setAntiAlias(true);
        priceLinePaint.setStrokeWidth(getResources().getDimension(R.dimen.dp1));
        lineColor = getResources().getColor(R.color.org);
        linePaint = new Paint();
        linePaint.setAntiAlias(true);
        linePaint.setColor(lineColor);
        linePaint.setStrokeWidth(getResources().getDimension(R.dimen.dp1));
        linePaint.setStyle(Paint.Style.STROKE);
        priceLinePaint.setColor(lineColor);
        paddingTop = (int) getResources().getDimension(R.dimen.dp3);
        paddingLeft = (int) getResources().getDimension(R.dimen.dp1);
        paddingBottom = (int) getResources().getDimension(R.dimen.dp7);
        paddingRight = (int) getResources().getDimension(R.dimen.dp3);
        radius = getResources().getDimension(R.dimen.dp3);
        radiusInner = getResources().getDimension(R.dimen.dp2);
        textTran = getResources().getDimension(R.dimen.dp5);
    }

    private void measureText(String text) {
        Rect rect = new Rect();
        textPaint = new TextPaint();

        sp10 = getResources().getDimension(R.dimen.sp10);
        textPaint.setTextSize(sp10);
        textPaint.setAntiAlias(true);
        textColor = getResources().getColor(R.color.text_gray);
        textPaint.setColor(textColor);
        textPaint.getTextBounds(text, 0, text.length(), rect);
        textWidthMin = rect.width();
        textHeightMin = rect.height();
        textPaint.setTextSize(sp10);
        textPaint.getTextBounds(text, 0, text.length(), rect);
        textWidthBig = rect.width();
        textHeightBig = rect.height();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        textPaint.setTextAlign(Paint.Align.LEFT);
        top = paddingTop + textHeightMin / 2;
        left = textWidthMin + paddingLeft;
        right = getWidth() - paddingRight - textWidthBig / 2;
        bottom = getHeight() - textHeightBig - paddingBottom;
        with = right - left;
        height = bottom - top;
        with0 = left;
        with1 = with * 2 / 3 + left;
        with2 = with / 3 + left;
        with3 = right;
        height0 = top;
        height1 = height * 2 / 3 + top;
        height2 = height / 3 + top;
        height3 = bottom;
        //四条横虚线
        linePath.moveTo(left, height0);
        linePath.lineTo(right, height0);
        linePath.moveTo(left, height1);
        linePath.lineTo(right, height1);
        linePath.moveTo(left, height2);
        linePath.lineTo(right, height2);
        linePath.moveTo(left, height3);
        linePath.lineTo(right, height3);
        //四条竖着的虚线
        linePath.moveTo(with0, top);
        linePath.lineTo(with0, bottom);
        linePath.moveTo(with1, top);
        linePath.lineTo(with1, bottom);
        linePath.moveTo(with2, top);
        linePath.lineTo(with2, bottom);
        linePath.moveTo(with3, top);
        linePath.lineTo(with3, bottom);
        canvas.drawPath(linePath, paint);

        //画价格
        textPaint.setTextSize(sp10);
        if (!TextUtils.isEmpty(price1) &&
                !TextUtils.isEmpty(price2) &&
                !TextUtils.isEmpty(price3) &&
                !TextUtils.isEmpty(price4)) {
            canvas.drawText(price1, 0, height0 + textHeightMin / 2, textPaint);
            canvas.drawText(price2, 0, height1 + textHeightMin / 2, textPaint);
            canvas.drawText(price3, 0, height2 + textHeightMin / 2, textPaint);
            canvas.drawText(price4, 0, height3 + textHeightMin / 2, textPaint);
        }


        //画日期
        textPaint.setTextSize(sp10);
        if (!TextUtils.isEmpty(timePoint1) &&
                !TextUtils.isEmpty(timePoint2) &&
                !TextUtils.isEmpty(timePoint3) &&
                !TextUtils.isEmpty(timePoint4)) {
            canvas.drawText(timePoint1, with0 - textWidthBig / 2, getHeight(), textPaint);
            canvas.drawText(timePoint3, with1 - textWidthBig / 2, getHeight(), textPaint);
            canvas.drawText(timePoint2, with2 - textWidthBig / 2, getHeight(), textPaint);
            canvas.drawText(timePoint4, with3 - textWidthBig / 2, getHeight(), textPaint);
        }
        drawPriceLine(canvas);

    }

    public void startLineAnim() {
        // 将 Path 与 PathMeasure 关联
// 截取一部分存入dst中，并使用 moveTo 保持截取得到的 Path 第一个点的位置不变
        measure = new PathMeasure(pathRes, false);
        if (valueAnimator != null && valueAnimator.isRunning()) {
            valueAnimator.cancel();
        }
        valueAnimator = ValueAnimator.ofFloat(0, measure.getLength());
        valueAnimator.setDuration(1500);
// 减速插值器
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                lineLength = (Float) animation.getAnimatedValue();
                measure.getSegment(0, lineLength, pathDst, true);
                postInvalidate();
            }
        });
        valueAnimator.start();
    }

    public void setDrawed(boolean drawed) {
        isDrawed = drawed;
        pathRes.reset();
        pathDst.reset();
        postInvalidate();
    }


    private void drawPriceLine(Canvas canvas) {
        currPriceHeight = bottom - currPrice / newCarGuidePrice * height;
        nextYearPriceHeight = bottom - nextYearPrice / newCarGuidePrice * height;
        priceLinePaint.setColor(lineColor);
        canvas.drawLine(with0, currPriceHeight, right, nextYearPriceHeight, priceLinePaint);
//        if (!isDrawed) {
//            isDrawed = true;
//            pathRes.moveTo(with0, currPriceHeight);
//            pathRes.lineTo(right, nextYearPriceHeight);
//            startLineAnim();
//        }
//        canvas.drawPath(pathDst, linePaint);
        float dy = nextYearPriceHeight - currPriceHeight;
        canvas.drawCircle(with0, currPriceHeight, radius, priceLinePaint);
        priceLinePaint.setColor(Color.WHITE);
        canvas.drawCircle(with0, currPriceHeight, radiusInner, priceLinePaint);

        priceLinePaint.setColor(lineColor);
        canvas.drawCircle(with2, currPriceHeight + dy / 3, radius, priceLinePaint);
        priceLinePaint.setColor(Color.WHITE);
        canvas.drawCircle(with2, currPriceHeight + dy / 3, radiusInner, priceLinePaint);

        priceLinePaint.setColor(lineColor);
        canvas.drawCircle(with1, currPriceHeight + dy / 3 * 2, radius, priceLinePaint);
        priceLinePaint.setColor(Color.WHITE);
        canvas.drawCircle(with1, currPriceHeight + dy / 3 * 2, radiusInner, priceLinePaint);

        priceLinePaint.setColor(lineColor);
        canvas.drawCircle(right, nextYearPriceHeight, radius, priceLinePaint);
        priceLinePaint.setColor(Color.WHITE);
        canvas.drawCircle(right, nextYearPriceHeight, radiusInner, priceLinePaint);


        textPaint.setTextAlign(Paint.Align.CENTER);
        canvas.drawText(currPrice + "", with0, currPriceHeight - textTran, textPaint);
        canvas.drawText(Utils.formatInt((nextYearPrice + (currPrice - nextYearPrice) / 3 * 2) + ""), with2, currPriceHeight + dy / 3 - textTran, textPaint);
        canvas.drawText(Utils.formatInt((nextYearPrice + (currPrice - nextYearPrice) / 3) + ""), with1, currPriceHeight + dy / 3 * 2 - textTran, textPaint);
        canvas.drawText(nextYearPrice + "", right, nextYearPriceHeight - textTran, textPaint);

    }

    public void initPrice(double newCarGuidePrice, double currPrice, double nextYearPrice) {

        price4 = Utils.format2Right(newCarGuidePrice / 3 * 0 + "");
        price2 = Utils.format2Right(newCarGuidePrice / 3 * 1 + "");
        price3 = Utils.format2Right(newCarGuidePrice / 3 * 2 + "");
        price1 = Utils.format2Right(newCarGuidePrice / 3 * 3 + "");
        this.newCarGuidePrice = (float) newCarGuidePrice;
        this.currPrice = (float) currPrice;
        this.nextYearPrice = (float) nextYearPrice;
        Log.e("tag", currPrice + "--" + nextYearPrice);
    }

    public void initTime(String time) {
        Long queryTime = Long.valueOf(DateUtils.getTime(time));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(queryTime * 1000l));
        year = calendar.get(Calendar.YEAR);//今年
        int month = calendar.get(Calendar.MONTH) + 1;
        if (month >= 10) {
            timePoint1 = year + "/" + month;
            timePoint4 = (year + 1) + "/" + month;
        } else {
            timePoint1 = year + "/0" + month;
            timePoint4 = (year + 1) + "/0" + month;
        }
        if (month + 4 > 12) {
            if ((month + 4 - 12) >= 10) {
                timePoint2 = (year + 1) + "/" + (month + 4 - 12);
            } else {
                timePoint2 = (year + 1) + "/0" + (month + 4 - 12);
            }
        } else {
            if ((month + 4) >= 10) {
                timePoint2 = year + "/" + (month + 4);
            } else {
                timePoint2 = year + "/0" + (month + 4);
            }
        }
        if (month + 8 > 12) {
            if ((month + 8 - 12) >= 10) {
                timePoint3 = (year + 1) + "/" + (month + 8 - 12);
            } else {
                timePoint3 = (year + 1) + "/0" + (month + 8 - 12);
            }
        } else {
            if (month + 8 >= 10) {
                timePoint3 = year + "/" + (month + 8);
            } else {
                timePoint3 = year + "/0" + (month + 8);
            }
        }
        postInvalidate();
    }
}
